package com.ksp.wke;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.ksp.api.KSPAPI_FileUtil;
import com.ksp.dom.KSPDOM_ResDta;
import com.sun.tools.internal.xjc.model.SymbolSpace;

import net.minidev.json.JSONArray;

@Controller
public class FileUploadController {

	@RequestMapping("/file/upload")
	@ResponseBody
	public KSPDOM_ResDta uploadFile(ServletRequest request) throws IOException, Exception {
		MultipartHttpServletRequest mreq = (MultipartHttpServletRequest)request;
		
		String wAttRotPth = "E:/hwk/task/top/ksp/upfile/";
		
		List<MultipartFile> wTskAttLst = mreq.getFiles("TSK_ATT");
		List<String> wAttPthLst = new ArrayList<>();
		for (int i = 0; i < wTskAttLst.size(); i++) {
			MultipartFile wTskAttTmp = wTskAttLst.get(i);
			String wRanUid = UUID.randomUUID().toString();
			String wOriNam = wTskAttTmp.getOriginalFilename();
			String wAttPth = wRanUid + wOriNam;
			KSPAPI_FileUtil.upload(wTskAttTmp.getBytes(), wAttRotPth, wAttPth);
			wAttPthLst.add(wAttPth);
		}
		System.out.println("上传附件地址：" );
		System.out.println(wAttPthLst);
		
		return KSPDOM_ResDta.buildSucc(JSONArray.toJSONString(wAttPthLst));
		
	}
	
}
